#include <stdio.h>
#include <string>

int main(){
  int n, ncand;
  char candidates[20][81];
  int ballots[1000][20];
  char enter;
  int temp;
  
  scanf("%d\n", &n);
  printf("\n");

  for(int k=0;k<n;k++){
    scanf("%d\n", &ncand);
    
    for(int i=0;i<ncand;i++){
      gets(candidates[i]);
    }

    int i=0;
    while(1){
      scanf("%d", &temp);
      if(temp=='\n'){
	printf("\n");
	break;
      }else{
	ballots[i][0]=temp;
	for(int j=1;j<ncand;j++){
	  scanf("%d", &ballots[i][j]);
	}
	scanf("%c", &enter);
      }
      i++;
    }
    int nballots =i;

    for(int m=0;m<nballots;m++){
      for(int n=0;n<ncand;n++){
	printf("%d ", &ballots[m][n]);
      }
      printf("\n");
    }
    

    
  }
}
